<template>
  <MyDrawer ref="myRef" title="参数" @submit="submit" :disabled="false" width="30%">
    <el-form :model="drawerProps.row" label-width="120" ref="formRef">
      <el-form-item label="参数名称">
        <el-input v-model="drawerProps.row.name"></el-input>
      </el-form-item>
      <el-form-item label="实际参数名称">
        <el-input v-model="drawerProps.row.paramName"></el-input>
      </el-form-item>
      <el-form-item label="参数类型">
        <el-input v-model="drawerProps.row.paramType"></el-input>
      </el-form-item>
    </el-form>
  </MyDrawer>
</template>

<script setup lang="ts">
import MyDrawer from "@/components/MyDrawer/index.vue";
import { ElMessage, FormInstance } from "element-plus";
import { ref } from "vue";

const myRef = ref<InstanceType<typeof MyDrawer> | null>();

interface DrawerProps {
  row: any;
  api?: (params: any) => Promise<any>;
  getTable?: () => Promise<any>;
}

const initData: any = {
  name: "",
  paramName: "",
  paramType: ""
};

const drawerProps = ref<DrawerProps>({ row: JSON.parse(JSON.stringify(initData)) });

const acceptParams = (params: any) => {
  drawerProps.value = params;
  myRef.value?.acceptParams();
};

const formRef = ref<FormInstance>();

const submit = () => {
  if (!formRef.value) return;
  formRef.value.validate(async valid => {
    if (!valid) return;
    await drawerProps.value.api!(drawerProps.value.row);
    ElMessage.success("成功");
    await drawerProps.value.getTable!();
    myRef.value?.close();
  });
};

defineExpose({
  acceptParams
});
</script>

<style></style>
